package com.zzg.srb.core.controller.admin;


import com.zzg.srb.common.exception.Assert;
import com.zzg.srb.common.result.R;
import com.zzg.srb.common.result.ResponseEnum;
import com.zzg.srb.core.entity.pojo.IntegralGrade;
import com.zzg.srb.core.service.IntegralGradeService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;


/**
 * <p>
 * 积分等级表 前端控制器
 * </p>
 *
 * @author zhengzg
 * @since 2022-05-27
 */
@RestController
@RequestMapping("/admin/core/integralGrade")
@Api(tags = "积分等级表")
public class AdminIntegralGradeController {

    private Logger log = LoggerFactory.getLogger(AdminIntegralGradeController.class);

    @Resource
    private IntegralGradeService integralGradeService;

    @ApiOperation("查询积分等级表")
    @GetMapping("/list")
    public R listAll(){
        return R.ok().data(integralGradeService.list());
    }

    @ApiOperation("根据id删除")
    @ApiImplicitParam(name = "id",value = "id",dataType = "Long")
    @DeleteMapping("/remove/{id}")
    public R removeById(@PathVariable Long id){
        log.info("根据id删除 ==>" + id);
        return integralGradeService.removeById(id) ? R.ok().message("删除成") : R.error().message("删除失败");
    }

    @ApiOperation("新增积分等级")
    @PutMapping("/save")
    @ApiImplicitParam(value = "integralGrade", dataType = "IntegralGrade", required = true, name = "积分等级对象")
    public R save(@RequestBody IntegralGrade integralGrade){
        log.info("新增积分等级 ==>" + integralGrade);
        //断言判断参数是否为null
        Assert.notNull(integralGrade.getBorrowAmount(),ResponseEnum.BORROW_AMOUNT_NULL_ERROR);
        return integralGradeService.save(integralGrade) ? R.ok().message("保存成功") : R.error().message("保存失败");
    }

    @ApiOperation("根据id获取数据")
    @GetMapping("get/{id}")
    @ApiImplicitParam(name = "id",value = "1",dataType = "Long")
    public R getById(@PathVariable Long id){
        log.info("根据id获取数据 ==>" + id);
        IntegralGrade integralGrade = integralGradeService.getById(id);
        return integralGrade != null ? R.ok().data(integralGrade) : R.error().message("数据获取失败");
    }

    @ApiOperation("更新积分等级")
    @PostMapping("/update")
    @ApiImplicitParam(value = "integralGrade", dataType = "IntegralGrade", required = true, name = "积分等级对象")
    public R update(@RequestBody IntegralGrade integralGrade){
        log.info("更新积分等级 ==>" + integralGrade);
        return integralGradeService.updateById(integralGrade) ? R.ok().message("更新成功") : R.error().message("更新失败");
    }
}

